#include <STC15F2K60S2.H>
#include "intrins.h"


#define FOSC 12000000L          
#define BAUD 4800         
#define S1_S0 0x40             
#define S1_S1 0x80              

bit busy;
bit rxd_flag;

unsigned char x;
unsigned char RX1_Buffer;
void IO_Init();
void SendData(unsigned char dat);
void SendString(char *s);




void main()
{
    IO_Init();
		SCON = 0x50; 
    ACC = P_SW1;
    ACC &= ~(S1_S0 | S1_S1);    
    P_SW1 = ACC;                
    AUXR = 0x40;                
    TMOD = 0x20;                
    TL1 = (256 - (FOSC/32/BAUD));   
    TH1 = (256 - (FOSC/32/BAUD));
    TR1 = 1;                   
    ES = 1;                    
    EA = 1;
	  RX1_Buffer=0;
	  P0=0x00;
    SendString("STC15\r\nUart Test !\r\n");
    while(1)
		{
		if(rxd_flag&&(!busy))		
		   {
       SendData(RX1_Buffer);
		   }  
		rxd_flag=0;  
    RX1_Buffer=0;
	
   }

}
void Uart() interrupt 4
{
    if (RI)
    {
        RI = 0;              
        P0 = SBUF;
			  RX1_Buffer=SBUF;		    	
        SBUF=RX1_Buffer;	
   		  P22 = RB8; 
    }
    if (TI)
    {
			TI = 0;                
      busy = 0; 
      			
    }
}

void IO_Init()
{	
    P0M0 = 0x00;
    P0M1 = 0x00;
    P1M0 = 0x00;
    P1M1 = 0x00;
    P2M0 = 0x00;
    P2M1 = 0x00;
    P3M0 = 0x00;
    P3M1 = 0x00;
    P4M0 = 0x00;
    P4M1 = 0x00;
    P5M0 = 0x00;
    P5M1 = 0x00;
    P6M0 = 0x00;
    P6M1 = 0x00;
    P7M0 = 0x00;
    P7M1 = 0x00;
}

void SendData(unsigned char dat)  
{
    while (busy);              
    ACC = dat;                  
    busy = 1;
    SBUF = ACC;                 
}

void SendString(char *s)
{
    while (*s)                  
    {
        SendData(*s++);        
    }
}


